﻿using System;
using System.Collections.Generic;
using System.Text;
using METODESA.Server.Data.Exceptions;
using METODESA.Server.Data.METODESA.Entity;
using NHibernate;
using NHibernate.Expression;

namespace METODESA.Server.Data.METODESA.Nhibernate
{
    public class ReparacionDao:BaseDao
    {
        public static IList<Servicio> getAllReparaciones()
        {
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Servicio));
                myCriteria.Add(NHibernate.Expression.Expression.Like("Tipo", "0"));
                IList<Servicio> reparaciones = myCriteria.List<Servicio>();
                return reparaciones;
            }
            catch (Exception e)
            {
                throw new ReparacionNotExistsException("There's no reparacion");
                return null;
            }
        }

        public static Servicio getReparacionesByCode_individual(int code)
        {

            Servicio reparacion = null;
            Boolean flag = true;
            myISession = NHibernateHelper.GetCurrentSession();

            try
            {
                reparacion = (Servicio)myISession.Load(typeof(Servicio), code);
                flag = false;
            }
            catch (HibernateException e)
            {
                throw new ReparacionNotExistsException("There's no reparacion with the provided code");
            }

            if (flag)
            {
                //throw new ClientNotExistsException("There's no client with the provided id");
                return null;
            }

            return reparacion;
        }


        public static IList<Servicio> getReparacionByCode(int code)
        {
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Servicio));
                myCriteria.Add(NHibernate.Expression.Expression.Like("Id", code)).Add(Expression.Like("Tipo", "0"));
                IList<Servicio> reparaciones = myCriteria.List<Servicio>();
                return reparaciones;
            }
            catch (Exception e)
            {
                throw new ReparacionNotExistsException("There's no reparacion with the provided code");
                return null;
            }
        }


        public static IList<Servicio> getReparacionesByDate(string date)
        {
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Servicio));
                myCriteria.Add(NHibernate.Expression.Expression.Like("FechaInicio",'%'+date+'%')).Add(Expression.Like("Tipo", "0"));
                IList<Servicio> reparaciones = myCriteria.List<Servicio>();
                return reparaciones;
            }
            catch (Exception e)
            {
                throw new ReparacionNotExistsException("There's no reparacion with the provided date");
                return null;
            }
        }

    }
}
